<?php

namespace v1;

use Base\Exception\NoRecordException;

class MyAddRess extends \ResourceClass
{
    public $isActive = true;
    public $remark = '';

    /**
     * 获取地址列表
     * @param array $req
     * @param array $post
     * @param $result
     */
    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid']) || !(int)$req['uid']) {
            return 'user_id不能为空';
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        $uid = (int)$req['uid'];
        try {
            $addressInfo = \Biz\User\User::getAddressByUid($uid);
            $result = array(
                'ship_username' => $addressInfo['ship_username'],
                'ship_mobile' => $addressInfo['ship_mobile'],
                'province_id' => $addressInfo['province_id'],
                'city_id' => $addressInfo['city_id'],
                'area_id' => $addressInfo['area_id'],
                'province' => $addressInfo['province'],
                'city' => $addressInfo['city'],
                'area' => $addressInfo['area'],
                'address' => $addressInfo['address'],
            );
        }catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 新增地址
     * @param array $req
     * @param array $post
     * @param $result
     */
    public function post(array $req, array $post, &$result)
    {
        if (!isset($req['user_id'])) {
            return 'user_id不能为空';
        }
        $checkRes = $this->autoCheck($req['user_id']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        try {
            $my_address_list = new \Orm\UserAddress\UserAddressList();
            $my_address_list->offsetSet('where','uid='.$req['user_id']);
            $my_address_list->load();
            if($my_address_list['lists']){
                $my_address = new \Orm\UserAddress\UserAddressClass();
                $my_address = $my_address_list['lists'][0]??$my_address;
            }else{
                $my_address = new \Orm\UserAddress\UserAddressClass();
            }
            $my_address['uid'] = $req['user_id'];
            $my_address['ship_username'] = $data['ship_username'];
            $my_address['ship_mobile'] = $data['ship_mobile'];
            $my_address['province_id'] = $data['province_id'];
            $my_address['city_id'] = $data['city_id'];
            $my_address['area_id'] = $data['area_id'];
            $my_address['province'] = $data['province'];
            $my_address['city'] = $data['city'];
            $my_address['area'] = $data['area'];
            $my_address['address'] = $data['address'];
            if($my_address['id']){
                $my_address->update();
            }else{
                $my_address->insert();
            }
            if(!$my_address['id']){
                throw new \Exception('操作失败');
            }
        }catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 编辑地址
     * @param array $req
     * @param array $post
     * @param $result
     */
    public function put(array $req, array $post, &$result)
    {
        /*if (!isset($req['t'])) {
            return '时间不能为空';
        }
        if (!isset($req['k'])) {
            return '公钥不能为空';
        }
        if (!isset($req['customer_id'])) {
            return '客户ID不能为空';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        try {
            $token = $req['t'];
            \Util\Rest::authCheck($req['customer_id'], $token, $req['k']);
            $my_address = new \Orm\CustomerAddress\CustomerAddressClass();
            $my_address['id'] = $data['id'];
            $my_address['customer_id'] = $req['customer_id'];
            $my_address['contact_name'] = $data['contact_name'];
            $my_address['contact_telephone'] = $data['contact_telephone'];
            $my_address['provice_id'] = $data['provice_id'];
            $my_address['city_id'] = $data['city_id'];
            $my_address['area_id'] = $data['area_id'];
            $my_address['adds_str'] = $data['adds_str'];
            $my_address['address'] = $data['address'];
            $my_address['is_default'] = \Orm\CustomerAddress\CustomerAddressClass::ADDS_DEFAULT;
            $my_address->update();
        }catch (\Exception $e) {
            return $e->getMessage();
        }*/
    }
}